#ifndef __PACKET_MODBUS_H__
#define __PACKET_MODBUS_H__
#include "PacketBase.h"

namespace controller
{
    static const int MODBUS_BUFFER_MAX = 500;
#pragma pack(1)
    typedef struct
    {
        uint8_t address;
        uint8_t cmd;
        uint8_t data[MODBUS_BUFFER_MAX - 4];
    } ModbusFrame;
#pragma pack()

    class PacketModbus : public PacketBase
    {
        PacketBaseHeader h;
       
        long last_frame_ms = 0; // 帧超时
        static const int frame_timeout_ms = 500;

    public: 
        ModbusFrame p;
        PacketModbus();
        ~PacketModbus();

        //接收
        void reset_recv();
        PacketBase *on_get(char *data, int size, void *arg = NULL); // 接收数据的处理

        //发送
        int to_bytes(char *dataout, void **arg = NULL);             // 生成一个字节流的发送包
        int build(uint8_t address, uint8_t cmd, uint16_t start_reg, uint8_t counter); // 多个register

        //打印
        std::string to_string(std::string arg="");                                    // 格式化包的输出

    };

};

#endif // ! __PACKET_MODBUS_H__
